package com.instalinks.android.smspostservice.activity;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.instalinks.android.smspostservice.AppTabBaseActivity;
import com.instalinks.android.smspostservice.R;
import com.instalinks.android.smspostservice.utils.enums.TimeOuts;

public class HomeActivity extends AppTabBaseActivity {

	private ImageView imgServiceStatus;
	private TextView txtSmsServiceEnabled;
	private TextView txtMissedCallServiceEnabled;

	private TextView txtUserType;
	private TextView txtValidityStatus;

	private TextView txtSmsSuccess;
	private TextView txtSmsInvalid;
	private TextView txtSmsDropped;
	private TextView txtSmsFailed;

	private TextView txtCallSuccess;
	private TextView txtCallFailed;

	private Button btnResetCounter;
	
	private Handler mHandler;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initialize();
	}

	private void initialize() {
		mAppCfg.rePopulate();

		try {
			setContentView(R.layout.home);
			// SERVICE STATUS
			this.imgServiceStatus = (ImageView) findViewById(R.id.hm_imgServiceStatus);

			this.txtSmsServiceEnabled = (TextView) findViewById(R.id.hm_txtSmsService);
			this.txtMissedCallServiceEnabled = (TextView) findViewById(R.id.hm_txtCallService);

			// SMS COUNT
			this.txtSmsSuccess = (TextView) findViewById(R.id.hm_txtSmsSuccess);
			this.txtSmsDropped = (TextView) findViewById(R.id.hm_txtSmsDropped);
			this.txtSmsInvalid = (TextView) findViewById(R.id.hm_txtSmsInvalid);
			this.txtSmsFailed = (TextView) findViewById(R.id.hm_txtSmsFailed);

			// CALL COUNT
			this.txtCallSuccess = (TextView) findViewById(R.id.hm_txtCallSuccess);
			this.txtCallFailed = (TextView) findViewById(R.id.hm_txtCallFailed);

			// SUBSCRIPTION
			this.txtUserType = (TextView) findViewById(R.id.hm_txtUserType);
			this.txtValidityStatus = (TextView) findViewById(R.id.hm_txtValidity);

			mHandler = new Handler();
			mHandler.removeCallbacks(statusUpdater);
			mHandler.post(statusUpdater);
			setValues();
		} catch (Exception e) {
			Log.e("[INSTALINKS]", e.getMessage());
		}
		
		btnResetCounter = (Button) this.findViewById(R.id.btnResetCounter);
		this.btnResetCounter.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				mAppCfg.resetCounters();
			}
		});

	}

	private void setValues() {
		mAppCfg.rePopulate();

		this.imgServiceStatus
				.setImageResource(mAppCfg.isServiceRunning() ? R.drawable.start_ico
						: R.drawable.stop_ico);

		this.txtSmsServiceEnabled
				.setText(mAppCfg.isSmsServiceFlag() ? "Enabled" : "Disabled");
		this.txtMissedCallServiceEnabled.setText(mAppCfg
				.isMissedServiceServiceFlag() ? "Enabled" : "Disabled");

		this.txtSmsSuccess
				.setText(String.valueOf(mAppCfg.getSmsSuccessCount()));
		this.txtSmsDropped
				.setText(String.valueOf(mAppCfg.getSmsDroppedCount()));
		this.txtSmsInvalid
				.setText(String.valueOf(mAppCfg.getSmsInvalidCount()));
		this.txtSmsFailed.setText(String.valueOf(mAppCfg.getSmsFailedCount()));

		this.txtCallSuccess.setText(String.valueOf(mAppCfg
				.getCallSuccessCount()));
		this.txtCallFailed
				.setText(String.valueOf(mAppCfg.getCallFailedCount()));

		this.txtUserType.setText((mAppCfg.isPaidFlag() ? "Paid" : "Demo"));
		this.txtValidityStatus.setText(mAppCfg.getValidityStatus());
	}

	private Runnable statusUpdater = new Runnable() {

		@Override
		public void run() {
			setValues();
			mHandler.postDelayed(this, TimeOuts.TwoSec.getValue());
		}
	};

	@Override
	protected boolean doBackgroundWork() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected void doOnSuccess() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void doOnFailure() {
		// TODO Auto-generated method stub
		
	}
}
